$(function() {
	
	var eventId;
	/*
	 * 加载型类
	 */
	var timestamp = Date.parse(new Date());
	$.ajax( {
		url : "/ag/zrt/loadLogType.action?ts="+timestamp,
		type : "POST",
		data : "",
		success : showData
	});
	/*提交*/
	$('#finish_tabs').click(function() {
		check();
	});
	/*返回*/
	$('#cancel_tabs').click(function() {
		window.close();
	});

	function showData(data) {
		if (data.logTypeList.length != 0) {
			$.each(data.logTypeList, function(i, info) {
				fillIntoHtml(info.ltId, info.ltName);
			});
		}
	}

	function fillIntoHtml(id, name) {
		html = "<option id=" + id + ">" + name + "</option>";
		$('#log_type').append(html);
	}

	/*
	 * 检查输入正确性
	 */
	function check() {
		var stepName = $('.newName').val();
		//alert(stepName);
		if (stepName == "") {
			alert("环节名称不可为空");
		} else {
			saveStep(stepName);
		}
	}
	/*
	 * 保存步骤
	 */
	function saveStep(stepName) {
		eventId = $('#eventId').text();
		var curStepId = $('#curStepId').text();
		
		//alert(eventId);
		//alert(curStepId);
		var timestamp = Date.parse(new Date());
		url = "/ag/zrt/add_LogStep.action?ts="+timestamp;
		data = {
			eventId : eventId,
			curStepId : curStepId,
			stepName : stepName
		};

		$.ajax( {
			url : url,
			data : data,
			type : "POST",
			success : saveAllContent//保存完新步骤后，再存对应内容
		});
	}
	
	function saveAllContent(data) {
		if ($("#tabs div").length != 0)//判断是否有内容
			saveEachContent(data);
	}
	/*
	 * 保存内容 
	 */
	function saveEachContent(data) {
		var newStepId = data.newStepId;
		//alert(newStepId);
		$("#tabs div").each(function() {
			// alert($(this).find('#typeId').text());
				// alert($(this).find('#newContent').text());
				var typeId = $(this).find('#typeId').text();
				var newContent = $(this).find('#newContent').text();
				var timestamp = Date.parse(new Date());
				url = "/ag/zrt/add_LogPsContent.action?ts="+timestamp;
				data = {
					typeId : typeId,
					newContent : newContent,
					newStepId : newStepId
				};

				$.ajax( {
					url : url,
					type : "POST",
					data : data
				});
			});
		//location.href = "/ag/zhh/chat/chatIndex.jsp?eventId=" + eventId;
		window.opener.location.reload();
		window.close();
		
		
		
	}
});